/*
 * create:2016年4月28日 上午9:33:25
 * author:wangds  
 */
package com.cxkj.shop.proc.order;

import java.util.Map;

import com.cxkj.shop.model.order.Order;
import com.cxkj.shop.model.order.OrderItem;
import com.cxkj.shop.model.user.User;
import com.cxkj.shop.utils.OrderUtils;
import com.cxkj.shop.utils.UserUtils;

import net.wangds.common.processor.AbstractInputOutputProcessor;
import net.wangds.utilities.lang.StringUtils;

/**
 * .
 * @author wangds
 *
 */
public class OrderGiftProc extends AbstractInputOutputProcessor<Map<String,Object>, Boolean>{

  public static final String KEY_TRADE_NO = "KEY_TRADE_NO";

  public static final String KEY_OPEN_ID = "KEY_OPEN_ID";
  
  public static final int KEY_GIFT_TYPE_THREE_FREE_ONE_MASK=1;
  public static final int KEY_GIFT_TYPE_ONE_FREE_ONE_MASK=2;
  
  
  private int giveCondition=1;
  
  private int giveAmount=1;
  
  private int giftType=3;
  
  
  
  
  /**
   * The getter method of the property giveAmount.
   * @author wangds 2016年4月28日 上午11:36:45.
   * @return the giveAmount.
   */
  public int getGiveAmount() {
    return giveAmount;
  }



  /**
   * The setter method of the property giveAmount.
   * @param thegiveAmount the giveAmount to set
   * @author wangds 2016年4月28日 上午11:36:45.
   */
  public void setGiveAmount(int giveAmount) {
    this.giveAmount = giveAmount;
  }



  /**
   * The getter method of the property giftType.
   * @author wangds 2016年4月28日 上午11:36:45.
   * @return the giftType.
   */
  public int getGiftType() {
    return giftType;
  }



  /**
   * The setter method of the property giftType.
   * @param thegiftType the giftType to set
   * @author wangds 2016年4月28日 上午11:36:45.
   */
  public void setGiftType(int giftType) {
    this.giftType = giftType;
  }



  /**
   * The getter method of the property giveCondition.
   * @author wangds 2016年4月28日 上午11:29:42.
   * @return the giveCondition.
   */
  public int getGiveCondition() {
    return giveCondition;
  }



  /**
   * The setter method of the property giveCondition.
   * @param thegiveCondition the giveCondition to set
   * @author wangds 2016年4月28日 上午11:29:42.
   */
  public void setGiveCondition(int giveCondition) {
    this.giveCondition = giveCondition;
  }



  /*
   * @see net.wangds.common.processor.AbstractInputOutputProcessor#process(java.lang.Object)
   * @author wangds 2016年4月28日 上午9:39:02.
   */
  @SuppressWarnings("unchecked")
  @Override
  protected Boolean process(Map<String, Object> map) throws Exception {
    boolean res = false;
    String id = (String)map.get(KEY_TRADE_NO);
    String open_id = (String)map.get(KEY_OPEN_ID);
    Order order = OrderUtils.queryOrderByOrderId(id);
    if(order==null){
      this.getLogger().debug("on giveing gift, order is null");
      return res;
    }
    int flag = 0;
    for(OrderItem cur:order.getItems()){
      flag+=cur.getType();
      flag+=cur.getFreeType();
    }
    
    if(flag>0){
      this.getLogger().debug(StringUtils.join("on giveing gift, flag is ",flag,"."));
      return res;
    }
    
    if(order.getTotalScore()<1 && order.getTotal()>=giveCondition){
      User u = null;
      if((this.giftType&KEY_GIFT_TYPE_THREE_FREE_ONE_MASK)==KEY_GIFT_TYPE_THREE_FREE_ONE_MASK){
        u=UserUtils.selectUserByOpenId(open_id);
        UserUtils.userObtainPay3Free1Card(u, giveAmount);
        res = true;
      }
      if((this.giftType&KEY_GIFT_TYPE_ONE_FREE_ONE_MASK)==KEY_GIFT_TYPE_ONE_FREE_ONE_MASK){
        if(u==null){
          u=UserUtils.selectUserByOpenId(open_id);
        }
        UserUtils.userObtainPay1Free1Card(u, giveAmount);
        res = true;
      }
    }else{
      this.getLogger().debug(StringUtils.join("on giveing gift fail, totalScore is ",order.getTotalScore(),", total is ",order.getTotal()));
    }
    
    return res;
  }


}
